package GUI.Ventas;

import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;

import sale.entities.OrderVO;
import sale.exceptions.EmptyOrderException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.exceptions.OutOfStockIngredientsException;

public class AgregarVentaMesa extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Text numeroMesa;
	OrderVO orden;
	Label error;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AgregarVentaMesa(Shell parent, int style, OrderVO o) {
		super(parent, style);
		orden=o;
		setText("Nueva Venta de Mesa");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(567, 346);
		shell.setText(getText());
		
		Label label = new Label(shell, SWT.NONE);
		label.setText("Articulos del pedido");
		label.setBounds(10, 61, 112, 14);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setBounds(10, 94, 479, 75);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("New Column");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("New Column");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(274);
		tableColumn_2.setText("New Column");
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setText("PRECIO TOTAL");
		label_1.setBounds(10, 214, 82, 14);
		
		Label precioTotal = new Label(shell, SWT.NONE);
		if (orden!=null && orden.getArts()!=null && orden.getArts().size()>0) {
			BigDecimal aux = new BigDecimal(0);
			for (ArticleVO o : orden.getArts())
				aux = aux.add(new BigDecimal(o.getPrice()));
			precioTotal.setText(aux.toString());
		} else
			precioTotal.setText("0");
		precioTotal.setBounds(158, 214, 59, 14);
		
		Label label_3 = new Label(shell, SWT.NONE);
		label_3.setVisible(true);
		label_3.setText("#Mesa");
		label_3.setBounds(10, 29, 39, 14);
		
		numeroMesa = new Text(shell, SWT.BORDER);
		numeroMesa.setVisible(true);
		numeroMesa.setBounds(55, 26, 64, 19);
		
		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(orden==null){
					ArrayList<ArticleVO> a = new ArrayList<ArticleVO>();
					ArrayList<String> d= new ArrayList<String>();
					orden = new OrderVO(a,d);
					if(numeroMesa.getText().equalsIgnoreCase("")){
						orden.setTableKey(-5);
					}
					else{
						orden.setTableKey(Integer.parseInt(numeroMesa.getText()));
					}
					System.out.println(orden.getTableKey());
					
					AgregarArticulosDelivery agregar = new AgregarArticulosDelivery(shell, shell.getStyle(), null, orden.getTableKey(),2, orden);
					//AgregarArticulosDelivery agregar = new AgregarArticulosDelivery(shell, shell.getStyle(), null, 0, 1, orden );
					
					shell.setVisible(false);
					try {
						agregar.open();
						
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					}
				}
				else{
					AgregarArticulosDelivery agregar = new AgregarArticulosDelivery(shell, shell.getStyle(), null, Integer.parseInt(numeroMesa.getText()),2, orden);
					shell.setVisible(false);
					try {
						agregar.open();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					}
					
				}
			}
		});
		button.setText("Agregar Articulo");
		button.setBounds(369, 175, 120, 28);
		
		Button button_1 = new Button(shell, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SaleProxyMgt uso = ServiceFacade.getInstance().getSaleProxyMgt();
				boolean errorr=false;
				System.out.println(orden.getEspecif().size());
				if(orden.getTableKey()==-5){
					if(numeroMesa.getText().equalsIgnoreCase("")){
						error.setText("Ingrese el numero de mesa!");
						errorr=true;
					}
					else{
						int t = Integer.parseInt(numeroMesa.getText());
						orden.setTableKey(t);
					}
				}
				if(errorr==false){
				try {
					
					uso.newSale(orden,orden.getTableKey());
					shell.close();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (EmptyOrderException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (OutOfStockIngredientsException e1) {
					ArrayList<ArticleVO> g =	e1.getArts();
					String m = "No se puede realizar el pedido. Ingredientes faltantes: ";
					for(int i = 0; i<g.size();i++){
					m = m + " " + g.get(i).getName();
					}
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), m);
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				
			}}
		});
		button_1.setText("Ingresar Orden");
		button_1.setBounds(377, 280, 112, 28);
		
		error = new Label(shell, SWT.NONE);
		error.setBounds(55, 287, 320, 14);
		error.setText("");
		
if(orden==null){
			
		}
		else{
			if(orden.getTableKey()==-5){
				
			}
			else{
			numeroMesa.setText(String.valueOf(orden.getTableKey()));}
			double precioT = 0;
			for (int i=0;i<orden.getArts().size();i++){
			//	System.out.print("hacemela facil");
				TableItem tableItem = new TableItem(table, SWT.NONE);
				tableItem.setText(0,orden.getArts().get(i).getName());
				tableItem.setText(1,String.valueOf(orden.getArts().get(i).getPrice()));
				tableItem.setText(2,orden.getEspecif().get(i));
				
				precioT=precioT + orden.getArts().get(i).getPrice();}
				precioTotal.setText(String.valueOf(precioT));
				
				
		}
	}
}
